<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!doctype html>
<html>
<head>
	<title>数据分析</title>
	<script type="text/javascript">
		var pageListUrl = "<c:url value="/report/service/list" />";

		var detailTbodyTemplateID = "detailTbodyTemplate";
		var detailTbodyID = "detailTbody";
		var pageToolID = "pageTool";

		var editTemplateID = "edit-template";
		var editFormID = "nsForm";
		var searchFormID = "searchForm";

		$(function () {
			reportGeneration();
		});

	</script>

</head>


<body  class="list-body">



<div class="content">
	<div class="title fix">
		<h3 class="b f14 fl">数据分析</h3>
		<div class="btns fr">
			<%--<button class="green" onclick="add()">新增</button>--%>
			<%--<button class="orange">返回</button>--%>
		</div>
	</div>

	<div class="main">
		<div class="inquiry">
			<form id="searchForm">
				<input type="hidden" name="dealerNum" value="${dealerNum}">

				<div class="group"><label>发送区间：</label>
					<input type="text" id="startDate" name="startDate" value="${currentMonth}" class="Wdate"  onFocus="WdatePicker({dateFmt:'yyyy-MM'})" autocomplete="off" />
				</div>
				<div class="group"><label>至：</label>
					<input type="text" id="endDate" name="endDate" value="${currentMonth}" class="Wdate"  onFocus="WdatePicker({dateFmt:'yyyy-MM'})" autocomplete="off" />
				</div>
				<div class="group"><label>保养类型：</label>
					<select name="maintenanceItem" id="maintenanceType">
						<option value="全部">全部</option>
						<option value="轮胎">轮胎</option>
						<option value="机油">机油</option>
						<option value="制动">制动</option>
					</select>
				</div>
				<div class="btns"><button type="button" class="bottleGreen vm" onclick="reportGeneration()">查询</button></div>
			</form>
		</div>

		<fieldset>
			<legend><label><strong>发送历史</strong></label></legend>
			<div class="table">
				<table width="100%">
					<thead>
					<tr>
						<%--<th width="50">序号</th>--%>
						<th width="">日期</th>
						<th width="">保养类型</th>
						<th width="">已发送消费者数量</th>
						<th width="">已发送短信数量</th>
					</tr>
					</thead>
					<tbody id="detailTbody" class="detailTr"></tbody>
				</table>
			</div>
		</fieldset>

		<fieldset>
            <legend><label><strong>客户回店率</strong></label></legend>
			<div class="table">
				<table width="100%">
					<thead>
					<tr>
						<th width="">日期</th>
						<th width="">保养类型</th>
						<th width="">已发送消费者数量</th>
						<th width="">30天回店消费者数量</th>
						<th width="">30天消费者回店率</th>
						<th width="">30天消费者回店消费金额</th>
						<th width="">60天回店消费者数量</th>
						<th width="">60天消费者回店率</th>
						<th width="">60天消费者回店消费金额</th>
						<th width="">90天回店消费者数量</th>
						<th width="">90天消费者回店率</th>
						<th width="">90天消费者回店消费金额</th>
					</tr>
					</thead>
					<tbody id="returnRateTbody" class="detailTr"></tbody>
				</table>
			</div>
        </fieldset>
	</div>
</div>

<script id="detailTbodyTemplate" type="text/html">
	{{each items as v i}}
	<tr id="{{v.id}}" >
		<%--<td>{{start + i + 1}}</td>--%>
		<td>{{v.sendDate}}</td>
		<td>{{v.maintenanceItem}}</td>
		<td>{{v.duplicatedCount}}</td>
		<td>{{v.totalCount}}</td>
	</tr>
	{{/each}}
	{{if items.length == 0}}
	<tr>
		<td colspan="4" class="none-data-tip">没有相关数据</td>
	</tr>
	{{/if}}
</script>

<script id="returnRateTemplate" type="text/html">
	{{each items as d i}}
	<tr id="{{d.id}}" >
		<td>{{d.sendDate}}</td>
		<td>{{d.maintenanceItem}}</td>
		<td>{{d.totalCount}}</td>
		{{if d.totalCount == 0}}
			<td>{{d.oneMonth}}</td>
			<td>{{100 * (d.oneMonth/ 1)}}%</td>
			<td>{{d.oneMonthAmount}}</td>
			<td>{{d.twoMonth}}</td>
			<td>{{100 * (d.twoMonth/ 1)}}%</td>
			<td>{{d.twoMonthAmount}}</td>
			<td>{{d.overTwo}}</td>
			<td>{{100 * (d.overTwo/ 1)}}%</td>
			<td>{{d.threeMonthAmount}}</td>
		{{else}}
			<td>{{d.oneMonth}}</td>
			<td>{{(100 * (d.oneMonth/ d.totalCount)).toPrecision(4)}}%</td>
			<td>{{d.oneMonthAmount}}</td>
			<td>{{d.twoMonth}}</td>
			<td>{{(100 * (d.twoMonth/ d.totalCount)).toPrecision(4)}}%</td>
			<td>{{d.twoMonthAmount}}</td>
			<td>{{d.overTwo}}</td>
			<td>{{(100 * (d.overTwo/ d.totalCount)).toPrecision(4)}}%</td>
			<td>{{d.threeMonthAmount}}</td>
		{{/if}}
	</tr>
	{{/each}}
	{{if items.length == 0}}
	<tr>
		<td colspan="5" class="none-data-tip">没有相关数据</td>
	</tr>
	{{/if}}
</script>

<script type="text/javascript">
function reportGeneration() {
	if(1 == validateDate())
		return;
	toPage();
	returnRate();
}
    function returnRate(){
        var $searchForm = $("#" + searchFormID);
        var nsIndex = ns.load("正在加载数据...");
        $.ajax({
            type: "POST",
            url: "<c:url value="/report/service/returnRatelist" />",
            data: $searchForm.serialize(),
            dataType: "json",
            async: true,
            cache:false,
            success: function(data){
                ns.close(nsIndex);
                if (data.success) {
                    var html = template("returnRateTemplate", data.data);
                    $("#returnRateTbody").html(html);
                } else {
                    ns.error(data.message);
                }

            },
            error: function(){
                ns.close(nsIndex);
                ns.error("操作失败，请重试！");
            }
        });
    }

function validateDate() {
	var startDate = $("#startDate").val();
	var endDate = $("#endDate").val();
	var startYear = 0, endYear = 0;
	if("" == startDate || "" == endDate) {
		alert("请选择查询日期");
		return 1;
    }
	
	startYear = parseInt(startDate.split('-')[0]);
	endYear = parseInt(endDate.split('-')[0]);
	var startMonth = parseInt(startDate.split('-')[1]);
	var endMonth = parseInt(endDate.split('-')[1]);
	//console.log(startDate + " " + startYear + " " + startMonth + " "+ endDate + " " + endYear + " " + endMonth + " " + (endMonth - startMonth));
	
	if(0 > endYear - startYear) {
		alert("结束日期不能小于起始日期");
		return 1;
	}
	if(0 > endMonth - startMonth) {
		alert("截止日期不能小于起始日期");
		return 1;
	}
	if(1 < endYear - startYear) {
		alert("时间跨度不能超过12个月");
		return 1;
	}
	if(1 == endYear - startYear && 12 < (12 - startMonth) + endMonth) {
		alert("时间跨度不能超过12个月");
		return 1;
	}
		
	return 0;
}
</script>
</body>
</html>




